<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Auth extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('globalmodel');
        $this->load->helper('url');
        $this->load->database();
    }
    public function index(){
        var_dump($this->session->userdata['token']);
    }

    public function open_login() {
        require_once APPPATH.'libraries/google-api-php-client/src/Google_Client.php';
        require_once APPPATH.'libraries/google-api-php-client/src/contrib/Google_PlusService.php';
        require_once APPPATH.'libraries/google-api-php-client/src/contrib/Google_Oauth2Service.php' ;
        require_once APPPATH.'libraries/google-api-php-client/src/contrib/Google_TasksService.php';

        $client = new Google_Client();


        $client->setApplicationName('Google+ PHP Starter Application');
// Visit https://code.google.com/apis/console?api=plus to generate your
// client id, client secret, and to register your redirect uri.
        $client->setClientId('1023179910439-l8ipdjlu5nes1c1tc79dp495nn4fripi.apps.googleusercontent.com');
        $client->setClientSecret('7_LacjQu-4ByvuNvXaa6KP-O');
        $client->setRedirectUri('http://canh.me/igoogle/index.php/auth/open_login');
        $client->setDeveloperKey('AIzaSyCvOCzhelwl3W4Mmlg2rHKvQiq2-yA8yqE');
        $client->setScopes(array('https://mail.google.com/mail/feed/atom','https://www.googleapis.com/auth/tasks', 'https://www.googleapis.com/auth/userinfo.profile' ));
        $plus = new Google_PlusService($client);
        $tasksService = new Google_TasksService($client);

        if (isset($_GET['code'])) {
            $client->authenticate();
            $this->session->set_userdata('token', $client->getAccessToken());

            $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
            header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
        }


        if ($this->session->userdata('token')) {
            $client->setAccessToken($this->session->userdata('token'));
        }

        if ($client->getAccessToken()) {
            $userdata = $this->globalmodel->login('ad@ad.com', '', $open_login = TRUE);

            $this->login_process($userdata);

            $this->session->set_userdata('token', $client->getAccessToken());

        } else {
            $authUrl = $client->createAuthUrl();
            header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
        }


    }


    public function login_process($userdata=null) {
        if ($userdata != null) {
            $newdata = array(
                'email' => $userdata->email,
                'userid' => $userdata->id,
                'logged_in' => TRUE
                );
            $this->session->set_userdata($newdata);
            if (isset($_POST['redirect'])) {
                redirect($_POST['redirect']);
            } else {
                redirect(base_url());
            }
        } else {
            $data['message'] = 'Lỗi đăng nhập!';
            $this->load->view('header');
            $this->load->view('message', $data);
            $this->load->view('footer');
        }
    }

    public function login() {
        if (isset($_POST['email'])) {
            $email = $_POST['email'];
            $password = $_POST['password'];


            $userdata = $this->globalmodel->login($email, $password);

            $this->login_process($userdata);
        } else {
            redirect(base_url());
        }
    }

    public function logout() {
        $newdata = array(
            'email' => '',
            'userid' => -1,
            'usergroup' => '',
            'logged_in' => FALSE,
            'token' => ''
            );
        $this->session->unset_userdata($newdata);
        if (isset($_REQUEST['redirect'])) {
            redirect($_REQUEST['redirect']);
        } else {
            redirect(base_url());
        }
    }

}